package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.SurfaceAmplitudeReq;
import com.ruibang.glass.produce.domain.resp.SurfaceAmplitude;
import com.ruibang.glass.produce.mapper.SurfaceAmplitudeMapper;
import com.ruibang.glass.produce.service.SurfaceAmplitudeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-01-31  18:52
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class SurfaceAmplitudeServiceImpl extends ServiceImpl<SurfaceAmplitudeMapper, SurfaceAmplitude> implements SurfaceAmplitudeService {

    @Override
    public IPage<SurfaceAmplitude> pageQuery(SurfaceAmplitudeReq surfaceAmplitudeReq) {
        IPage<SurfaceAmplitude> iPage = baseMapper.pageQuery(new Page<>(surfaceAmplitudeReq.getPageNo(), surfaceAmplitudeReq.getPageSize()), surfaceAmplitudeReq);
        if (iPage != null && CollectionUtils.isNotEmpty(iPage.getRecords())) {
            List<SurfaceAmplitude> data = new ArrayList<>();
            List<SurfaceAmplitude> surfaceAmplitudeList = iPage.getRecords();
            Map<String, List<SurfaceAmplitude>> surfaceAmplitudeMap = surfaceAmplitudeList.stream().collect(Collectors.groupingBy(SurfaceAmplitude::getGlassId));
            surfaceAmplitudeMap.forEach((k, v) -> {
                SurfaceAmplitude surfaceAmplitude = new SurfaceAmplitude();
                surfaceAmplitude.setGlassId(k);
                for (SurfaceAmplitude amplitude : v) {
                    if (surfaceAmplitude.getCollectTime() == null) {
                        surfaceAmplitude.setCollectTime(amplitude.getCollectTime());
                    }
                    switch (amplitude.getCamera()) {
                        case Constants.ZERO_STR: {
                            surfaceAmplitude.setH4(amplitude.getData());
                            break;
                        }
                        case Constants.ONE_STR: {
                            surfaceAmplitude.setH3(amplitude.getData());
                            break;
                        }
                        case Constants.TWO_STR: {
                            surfaceAmplitude.setH1(amplitude.getData());
                            break;
                        }
                        case Constants.THREE_STR: {
                            surfaceAmplitude.setH2(amplitude.getData());
                            break;
                        }
                        default: {
                            break;
                        }
                    }
                }
                data.add(surfaceAmplitude);
            });
            iPage.setRecords(data.stream().sorted(Comparator.comparing(SurfaceAmplitude::getCollectTime).reversed()).collect(Collectors.toList()));
        }
        return iPage;
    }

}
